package com.hnust.controller;

import com.hnust.domain.accept.PaperData;
import com.hnust.domain.record.QuestionTypeRecord;
import com.hnust.domain.record.RepeatQues;
import com.hnust.domain.record.SubjectInfo;
import com.hnust.domain.record.*;
import com.hnust.domain.response.PaperInfo;
import com.hnust.service.PaperService;
import com.hnust.service.TokenService;
import com.hnust.util.Result;
import com.hnust.util.Tools;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建人：gh
 * 创建时间：2020/10/29
 * 描述：试卷管理
 */
@CrossOrigin("*")
@RestController
@RequestMapping("api/teacher/testpaper")
public class PaperController {

    @Autowired
    private PaperService paperService;
    @Autowired
    private TokenService tokenService;


    // 删除试卷
    @GetMapping("/delTestPaper")
    public Result<Integer> delTestPaper(@RequestParam("token") String token,  @RequestParam("paper_ids") List<String> paper_ids){
        try {
            if(tokenService.verifyToken(token)){
                for(String paper_id : paper_ids) {
                    if (!paperService.delTestPaper(paper_id)) {
                        return new Result<>(0, "删除失败", null);
                    }
                }
                return new Result<>(1,"删除成功",null);
            }else{
                return new Result<>(0,"没有操作权限,请登录",null);
            }
        }catch (Exception e){
            return new Result<>(0,"操作异常，请稍后再试",null);
        }
    }

    // 获取试卷列表
    @GetMapping("/getPaperList")
    public Result<PaperInfo> getPaperList(
//            @RequestParam("token") String token,
            @RequestParam("id") String id,
			@RequestParam(value = "course_id",required = false) String course_id,
            @RequestParam("now_page") int now_page
    ) {
        try {
//            if(tokenService.verifyToken(token)){
                // 这里要判断course_id是不是null如果是null表示获取全部课程的，如果不是则获取指定课程的
            PaperInfo result = new PaperInfo();
            result.setCount(paperService.getPaperListCount(course_id, id));
            result.setSubject(paperService.getPaperList(id, course_id, now_page));
            return new Result<PaperInfo>(1,"获取成功", result);
//            }else{
//                return new Result<>(0,"没有操作权限,请登录",null);
//            }
        }catch (Exception e){

            return new Result<>(0,"操作异常，请稍后再试",null);
        }
    }

    /**
     * 将试卷设为样卷
     * @param token token
     * @param paper_ids 试卷Ids
     * @return 结果
     */
    @GetMapping("/changePaperType")
    public Result<Integer> changePaperType(@RequestParam("token") String token,@RequestParam("paper_ids") List<String> paper_ids){
        try {
            if(tokenService.verifyToken(token)){
                for(String paper_id : paper_ids) {
                    if (!paperService.setPaperToSample(paper_id)) {
                        return new Result<>(0, "修改失败", null);
                    }
                }
                return new Result<>(1, "修改成功", null);
            }else{
                return new Result<>(0,"没有操作权限,请登录",null);
            }
        }catch (Exception e){
            return new Result<>(0,"操作异常，请稍后再试",null);
        }
    }

    /**
     * 取消试卷设为样卷
     * @param token token
     * @param paper_ids 试卷Ids
     * @return 结果
     */
    @GetMapping("/removePaperType")
    public Result<Integer> removePaperType(@RequestParam("token") String token,@RequestParam("paper_ids") List<String> paper_ids){
        try {
            if(tokenService.verifyToken(token)){
                for(String paper_id : paper_ids) {
                    if (!paperService.removePaperToSample(paper_id)) {
                        return new Result<>(0, "修改失败", null);
                    }
                }
                return new Result<>(1,"修改成功",null);
            }else{
                return new Result<>(0,"没有操作权限,请登录",null);
            }
        }catch (Exception e){
            return new Result<>(0,"操作异常，请稍后再试",null);
        }
    }

    /**
     * 手动保存试卷
     * @param data 试卷信息
     * @return 是否保存成功
     */
    @PostMapping("/saveCompletePaper")
    public Result<Boolean> saveCompletePaper(@RequestBody PaperData data){

        try {
            if (!tokenService.verifyToken(data.getToken())){
                return new Result<>(0,"没有操作权限,请登录",null);
            }
            if (paperService.saveCompletePaper(data)){
                return new Result<>(1,"保存成功",null);
            }
            return new Result<>(0,"保存失败",null);
        } catch (Exception e){
            return new Result<>(0,"保存失败",null);
        }
    }

    /**
     * 获取题目类型列表
     * @param token token
     * @return 结果
     */
    @GetMapping("/getQuesTypeList")
    public Result<List<QuestionTypeRecord>> getQuesTypeList(
            @RequestParam("token")String token
    ){
        try {
            if (!tokenService.verifyToken(token)){
                return new Result<>(0,"没有操作权限,请登录",null);
            }
            List<QuestionTypeRecord> records = paperService.getAllQuestionType();
            return new Result<>(1,"获取题型成功",records);
        } catch (Exception e){
            return new Result<>(0,"查询时出现异常",null);
        }
    }

    /**
     *获取某个课程，某个题型的所有题目
     * @param token token
     * @param course_id 课程id
     * @param type_id 题目类型id
     * @param now_page 当前第几页
     * @return 结果
     */
    @GetMapping("/getCourseQuesList")
    public Result<SubjectInfo> getCourseQuesList(
            @RequestParam("token") String token,
            @RequestParam("course_id") String course_id,
            @RequestParam("type_id") String type_id,
            @RequestParam("now_page") int now_page
    ){
        try {

            if (!tokenService.verifyToken(token)){
                return new Result<>(0,"没有操作权限,请登录",null);
            }
            return new Result<>(1,"获取题目成功", paperService.getCourseQuesList(course_id, type_id, now_page));
        }catch (Exception e){
            return new Result<>(0,"查询时出现异常",null);
        }
    }


    /**
     * 搜索某个课程，某个题型的题目
     * @param token token信息
     * @param id 老师id
     * @param courseId 课程id
     * @param typeId 题目类型id
     * @param keyword 关键词
     * @param diff 难度
     * @param now 当前页数
     * @return 所有题目
     */
    @GetMapping("/getQuesByCon")
    public Result<SubjectInfo> getQuesByCon(
            @RequestParam("token") String token,
            @RequestParam("id") String id,
            @RequestParam("course_id") String courseId,
            @RequestParam("type_id") String typeId,
            @RequestParam("keyword") String keyword,
            @RequestParam("now_page") Integer now,
            @RequestParam("diff") Integer diff
    ){
        try {
            if (!tokenService.verifyToken(token)){
                return new Result<>(0,"没有操作权限,请登录",null);
            }
            SubjectInfo subjectInfo = paperService.searchQuestionByCon(courseId,typeId,keyword,now,diff);
            return new Result<>(1,"获取题型成功",subjectInfo);
        } catch (Exception e){
            return new Result<>(0,"查询过程中出现异常",null);
        }
    }

    /**
     * 查重试卷获取重复的题目
     * @param token token
     * @param id id
     * @param courseId 课程id
     * @param subjectIds 题目id
     * @return 结果
     */
    @GetMapping("/checkPaperRepeat")
    public Result<List<RepeatQues>> checkPaperRepeat(
            @RequestParam("token") String token,
            @RequestParam("id") String id,
            @RequestParam("course_id") String courseId,
            @RequestParam("subject_ids") List<String> subjectIds

    ){
        try {
            if (!tokenService.verifyToken(token)){
                return new Result<>(0,"没有操作权限,请登录",null);
            }
            List<RepeatQues> repeat = paperService.checkPaperRepeat(courseId,subjectIds);
            return new Result<>(1,"获取题型成功",repeat);
        } catch (Exception e){
            return new Result<>(0,"查询过程中出现异常",null);
        }
    }

    /**
     * 自动生成试卷
     * @param course_id 课程Id
     * @param list list数组
     * @param token 验证token
     * @return 结果
     */
    @PostMapping("/getAutoPaper")
    public Result<List<SubjectData>> getAutoPaper(@RequestPart("course_id")String course_id,@RequestPart("token") String token,@RequestPart("list") List<PaperQues> list){
        try {
            //前端使用Multipart/json传递数据，导致字符串型数据被多加一对"",需要手动删除
            String course_Id=course_id.replaceAll("\"","");
            String Token=token.replaceAll("\"","");
            if(tokenService.verifyToken(Token)){
                Result<List<SubjectData>> result=new Result<List<SubjectData>>(1,"自动生成试卷操作成功", paperService.getAutoPaper(list, course_Id));
                return result;
            }else{
                return new Result<>(0,"没有操作权限,请登录",null);
            }
        }catch (Exception e){

            return new Result<>(0,"操作异常，请稍后再试",null);
        }
    }

    @GetMapping("/setDisableQue")
    public Result<Boolean> setDisableQue(
//            @RequestParam("course_id") String courseId,
            @RequestParam("subject_ids") List<String> subjectIdList
        ){
        Boolean result = false;
        String msg = "设置成功";
        int code = 1;
        try{
            result = paperService.setDisableQue(subjectIdList);
        }catch (Exception e){
            msg = "设置失败 错误提示:"+e.getMessage();
            code = 0;
        }
        return new Result<>(code,msg,result);
    }
}
